﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _180FrameWork.CustControls
{
    public partial class frmDataGridViewColumnSetting : Form
    {
        public string option { get;private set; }

        //private CustDataGridViewStyle _style { get; set; }

        //public frmDataGridViewColumnSetting(CustDataGridViewStyle style)
        //{
        //    InitializeComponent();
        //    this.dg.AutoGenerateColumns = false;
        //    _style = style;
        //    this.bs.DataSource = _style.ColumnStyleList;
        //}

        private void frmDataGridViewColumnVisable_Load(object sender, EventArgs e)
        {
        }
        /// <summary>
        /// 列序上移
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUp_Click(object sender, EventArgs e)
        {
            if (this.bs.Current != null)
            {
                var item = this.bs.Current as CustDataGridViewColumnStyle;
                var index = this.bs.IndexOf(item);
                if (index > 0)
                {
                    this.bs.RemoveAt(index);
                    this.bs.Insert(index - 1, item);
                    this.bs.MovePrevious(); 
                    this.bs.MovePrevious();
                    this.bs.ResetBindings(false);
                }
            }          
        }
        /// <summary>
        /// 列序下移
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDown_Click(object sender, EventArgs e)
        {
            if (this.bs.Current != null)
            {
                var item = this.bs.Current as CustDataGridViewColumnStyle;
                var index = this.bs.IndexOf(item);
                if (index < (this.bs.Count - 1))
                {
                    this.bs.RemoveAt(index);
                    this.bs.Insert(index + 1, item);
                    this.bs.MoveNext();
                    this.bs.ResetBindings(false);
                }
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
     
        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dg.Rows.Count; i++)
            {
                var item = this.dg.Rows[i].DataBoundItem as CustDataGridViewColumnStyle;
                item.DisplayIndex = i;
            }
            this.DialogResult = DialogResult.OK;
            this.option = "change";
        }

        private void btnReload_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("重置所有列样式？", "提问", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                this.DialogResult = DialogResult.OK;
                this.option = "reload";
            }
        }
    }
}
